/**
 * ZipUtils.java
 * Created at 2017-4-5
 * Created by shihui
 * Copyright (C) 2017 BROADTEXT SOFTWARE, All rights reserved.
 */
package com.broadtext.common.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import com.broadtext.common.bean.FileBean;

/**
 * <p>ClassName: ZipUtils</p>
 * <p>Description: 文件压缩工具类</p>
 * <p>Author: shihui</p>
 * <p>Date: 2017-4-5</p>
 */

public class ZipUtils {
    
    /**
     * 
     * <p>Description: 一次性压缩多个文件，文件存放至一个文件夹中</p>
     * @param filepath 要被压缩的文件夹
     * @param zippath 保存路径及文件名
     */
    public static void zipMultiFile(String filepath, String zippath) {
        File file;
        file = new File(filepath);
        File zipFile;
        zipFile = new File(zippath);
        InputStream input = null;
        ZipOutputStream zipOut;
        try {
            zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
            if (file.isDirectory()) {
                File[] files;
                files = file.listFiles();
                for (int i = 0; i < files.length; ++i) {
                    input = new FileInputStream(files[i]);
                    zipOut.putNextEntry(new ZipEntry(file.getName() + File.separator + files[i].getName()));
                    int temp = 0;
                    while ((temp = input.read()) != -1) {
                        zipOut.write(temp);
                    }
                    input.close();
                }
            }
            zipOut.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 
     * <p>Description: 一次性压缩多个文件，文件存放至一个文件夹中</p>
     * @param fileList 要被压缩的文件
     * @param zippath 压缩后保存路径
     */
    public static void zipMultiFile(List<FileBean> fileList, String zippath) {
        File zipFile;
        zipFile = new File(zippath);
        ZipOutputStream zipOut;
        try {
            if (zipFile.getParent() != null && !new File(zipFile.getParent()).exists()) {
                new File(zipFile.getParent()).mkdirs();
            }
            zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
            InputStream input = null;
            Iterator<FileBean> it;
            it = fileList.iterator();
            while (it.hasNext()) {
                FileBean fileBean;
                fileBean = it.next();
                String path;
                path = fileBean.getFilePath() + fileBean.getFileName();
                File file;
                file = new File(path);
                if (file.isFile()) {
                    input = new FileInputStream(file);
                    zipOut.putNextEntry(new ZipEntry(file.getName()));
                    int temp = 0;
                    while ((temp = input.read()) != -1) {
                        zipOut.write(temp);
                    }
                    input.close();
                }
            }
            zipOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
